package it.infodea.tapestrydea.components;

import org.apache.tapestry5.BindingConstants;
import org.apache.tapestry5.MarkupWriter;
import org.apache.tapestry5.annotations.IncludeStylesheet;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.corelib.components.BeanEditor;
import org.apache.tapestry5.corelib.components.FormFragment;
import org.apache.tapestry5.dom.Element;

@IncludeStylesheet(value = "../assets/styles/tapestrydea.css")
public class BeanEditorDea extends BeanEditor {

	/**
	 * The label of the legend in the fieldset form. The field is adding only if
	 * the legendLabel is setted.
	 */
	@Parameter(defaultPrefix = BindingConstants.LITERAL)
	@Property(write = false)
	private String legendLabel;

	/**
	 * The label of the help in the fieldset form.
	 */
	@SuppressWarnings("unused")
	@Parameter(defaultPrefix = BindingConstants.LITERAL)
	@Property(write = false)
	private String helpLabel;

	/**
	 * If true the beaneditor is 'fragmented'. See {@link FormFragment}.
	 */
	@SuppressWarnings("unused")
	@Parameter
	@Property(write = false)
	private boolean fragment;

	/**
	 * Only if fragment is true. Determines if the fragment is initially visible or initially invisible
	 * (the default). See {@link FormFragment}. Remember to persist passed value.
	 */
	@SuppressWarnings("unused")
	@Parameter
	@Property
	private boolean fragmentVisible;

	/**
	 * Only if fragment is true. The text label for the fragment check, by default "Show".
	 */
	@SuppressWarnings("unused")
	@Parameter(value = "message:fragment-label", defaultPrefix = BindingConstants.LITERAL)
	@Property(write = false)
	private String fragmentLabel;

	/**
	 * Name of a function on the client-side Tapestry.ElementEffect object that
	 * is invoked to make the fragment visible. If not specified, then the
	 * default "show" function is used.
	 */
	@SuppressWarnings("unused")
	@Parameter(value = "show", defaultPrefix = BindingConstants.LITERAL)
	@Property(write = false)
	private String show;

	/**
	 * Name of a function on the client-side Tapestry.ElementEffect object that
	 * is invoked when the fragment is to be hidden. If not specified, the
	 * default "fade" function is used.
	 */
	@SuppressWarnings("unused")
	@Parameter(value = "fade", defaultPrefix = BindingConstants.LITERAL)
	@Property(write = false)
	private String hide;

	void beginRender(MarkupWriter writer) {
		if (legendLabel != null) {
			Element e = writer.element("fieldset");
			e.addClassName("dea-margin");
			writer.element("legend");
			writer.writeRaw(legendLabel);
			writer.end();
		}
	}

	void afterRender(MarkupWriter writer) {
		if (legendLabel != null) {
			writer.end();
		}
	}

}
